(function ($) {
	var examInfo,perDict,abnormalDict,ulIndex;
	$.init();
	$.plusReady(function () {
	    baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	
	function getAbnormalDetail(){
		var pdict = {'userId':perDict['userId'],'releaseId':perDict['releaseId']}
		var paramDict = {'method':'web.INMTeaComm:GetTakeException','params':JSON.stringify(pdict),'type':'RecMethod'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var dataDict = result.data
			if (!dataDict) {
				mui.toast('请重试')
				return;
			}
			abnormalDict = dataDict
			//{"ExceptionDR":"75||2","ReleaseId":"62","Remark":"异常说明的备注","ScoreUsedFlag":"N","UserDR":"4","WardDR":"1"}
			var dom =  $('#abnormalType li[data-id="'+abnormalDict['ExceptionDR']+'"]')[0]
			if (dom) {
				dom.classList.add('dhc-active')
			}
			$('#markContent')[0].value = dataDict['Remark']
			$('#textNum')[0].innerText = $('#markContent')[0].value.length
			if (dataDict['ScoreUsedFlag'] == 'Y') {
				$('#static')[0].classList.add('mui-active')
			}
		}, function(errorStr) {
		 	mui.toast('请重试')
		});
	}
	function saveAbnormalDetail(){
		var dom =  $('#abnormalType .dhc-active')[0]
		if (!dom) {
			mui.toast('请选择异常说明')
			return
		}
		var flag = 'N'
		var staticStr = $('#static')[0].getAttribute('class')
		if ( staticStr.indexOf('mui-active') != -1) {
			flag = 'Y'
		}
		var parr = 'ExceptionDR|'+dom.getAttribute('data-id')+'^Remark|'+$('#markContent')[0].value+'^ScoreUsedFlag|'+flag
			+'^WardDR|'+abnormalDict['WardDR']+'^UserDR|'+abnormalDict['UserDR']+'^ReleaseId|'+abnormalDict['ReleaseId']+'^userName|^'
		var pdict = {'parr':parr}
		var paramDict = {'method':'web.INMTeaComm:SaveTakeException','params':JSON.stringify(pdict),'type':'Method'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			if (result['msg']!='1') {
				mui.toast('请重试')
				return;
			}
			var operList = plus.webview.getWebviewById('operList')
			if (operList) {
				operList.evalJS("refreshInExamItemData('"+ulIndex+"')")
			}
			mui.back()
		}, function(errorStr) {
		 	mui.toast('请重试')
		});
	}
	function findPubCode(){
		var pdict = {'type':'考核异常'}
		var paramDict = {'method':'web.INMDBComm:FindPubCode','params':JSON.stringify(pdict),'type':'Query'}
		mui.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var dataArr = result['data']
			var html = ''
			for (var i = 0;i < dataArr.length;i++) {
				var oneCode = dataArr[i]
				html += '<li class="mui-table-view-cell" data-id="'+oneCode['SubValue']+'">'+oneCode['SubDesc']+'</li>'
			}
			$('#abnormalType')[0].innerHTML = html
			if (abnormalDict) {
				var dom =  $('#abnormalType li[data-id="'+abnormalDict['ExceptionDR']+'"]')[0]
				if (dom) {
					dom.classList.add('dhc-active')
				}
			}
		}, function(errorStr) {
		 	mui('#optionsPopo').popover('hide')
			mui.toast('请重试')
		});
	}
	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		$('body').on('tap','.baseBtn',saveAbnormalDetail)
		var self = plus.webview.currentWebview()
		examInfo = self.examInfo
		perDict = self.perDict
		ulIndex = self.ulIndex
		$('#titleInfo')[0].innerText = perDict['userName']+'【'+perDict['wardDesc']+'】'
		var perInfo = app.getState()
		// nurseid = perInfo['loginPerID']
		findPubCode()
		getAbnormalDetail()
		$('#abnormalType').on('tap','.mui-table-view-cell',changeAbnormalType)
		$('.markArea').on('input','#markContent',textAreaTextNum)
		$('.markArea').on('propertychange','#markContent',textAreaTextNum)
		
	}
	function textAreaTextNum() {
		var valueStr = this.value;
		var length = valueStr.length;
		var textNum = $('#textNum')[0];
		textNum.innerText = length
	}
	
	function changeAbnormalType(){
		var oldActive = $('#abnormalType .dhc-active')[0]
		if (oldActive == this) {
			return
		}
		if (oldActive) {
			oldActive.classList.remove('dhc-active')
		}
		this.classList.add('dhc-active')
	}
})(mui)

